from django.core import serializers
from django.http import JsonResponse, HttpResponse

from CoreSubSystem.models import user


def get_all_users(request):
    '''
        根据用户id返回用户对象列表
    :param request: get形式的request，包括了user_id
    :return: 列表形式的用户个人对象
    '''
    id = request.GET.get("user_id")
    finded_user = user.objects.get(id=id)
    finded_user.user_password = "******"
    finded_user = [finded_user]
    return_json = serializers.serialize("json", finded_user)
    return_object = {"flag" : True,"data": return_json}
    return JsonResponse(return_object)

def login(request):
    print(request.POST)
    user_id = request.POST.get("user_name")
    user_password = request.POST.get("user_password")
    try:
        login_user = [user.objects.filter(user_password=user_password).get(id=user_id)]
    except Exception:
        login_user = []
    flag = False
    return_json = serializers.serialize("json", login_user)
    if len(login_user) != 0:
        flag = True
    data = {'data': return_json, 'flag': flag}
    return JsonResponse(data)
